package com.dc.commons.utils.excel;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.dc.commons.dict.Dictionary;
import com.dc.commons.helper.DateHelper;
import com.dc.commons.persistence.embeddable.Dict;
import com.dc.commons.utils.Reflections;
import com.fasterxml.jackson.annotation.JsonFormat;

/*******************************************************************************<P>
 * 文件名: ExcelCovert.java <br>
 * 版本: 1.0<br>
 * 描述:<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年6月7日 下午12:52:38<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
public class ExcelCovert{

	
	public static class DefaultImportFormat<T> implements ImportFormat<T>{
		
		/**
		 * 字段列表
		 */
		private List<String> fields;
		
		/**
		 * 类名称
		 */
		private Class<T> clazz;
		
		public DefaultImportFormat(List<String> fields,Class<T> clazz){
			this.fields=fields;
			this.clazz=clazz;
		}
		
		/* (non-Javadoc)
		 * @see com.dc.commons.utils.excel.ImportFormat#convert(com.dc.commons.utils.excel.Row)
		 */
		@Override
		public T convert(RowData row) {
			T t=null;
			try {
				t=clazz.newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			
			for(int i=0;i<row.getCells().size();i++){
				Object o=row.getCells().get(i).getData();
				String fieldName=fields.get(i);
				Reflections.invokeSetter(t, fieldName, o);
			}
			
			return t;
		}
		
	}
	
	public static class DefaultExportFormat<T> implements ExportFormat<T>{
		/**
		 * 标题名称
		 */
		private String title;
		
		/**
		 * 字段名称
		 */
		private String field;
		
		public DefaultExportFormat(String title,String field){
			this.title=title;
			this.field=field;
		}
		/* (non-Javadoc)
		 * @see com.dc.commons.utils.excel.ExportFormat#getTitle()
		 */
		@Override
		public String getTitle() {
			return title;
		}

		/* (non-Javadoc)
		 * @see com.dc.commons.utils.excel.ExportFormat#formatValue(java.lang.Object)
		 */
		@Override
		public Object formatValue(T obj) {
			Object o= Reflections.invokeGetter(obj, field);
			if(o instanceof Date){
				Date date=(Date)o;
				Method method=Reflections.getAccessibleMethodByName(obj, "get"+StringUtils.capitalize(field));
				if(method.isAnnotationPresent(JsonFormat.class)){
					JsonFormat format=method.getAnnotation(JsonFormat.class);
					return DateHelper.format(date, format.pattern());
				}else{
					return DateHelper.formatTime(date);
				}
			}
			if(o instanceof Dictionary){
				return ((Dictionary)o).getName();
				
			}
			if(o instanceof Dict){
				return ((Dict)o).getName();
			}
			if(o instanceof Number){
				Number num=(Number)o;
				/*Method method=Reflections.getAccessibleMethodByName(t, "get"+StringUtils.capitalize(field));
				if(method.isAnnotationPresent(JsonFormat.class)){
					//JsonFormat format=method.getAnnotation(JsonFormat.class);
					//num=NumberUtils.createNumber(arg0);
				}*/
				return num;
			}
			return o==null?"":o.toString();
		}
	}
	

}
